import AMapLoader from "@amap/amap-jsapi-loader";
const load = () => {
    return new Promise(resolve => {
        AMapLoader.load({
            key: "788bc2886be41fdafa1879d9906e8e5d", // 申请好的Web端开发者Key，首次调用 load 时必填
            version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
            // plugins: ["AMap.Geolocation"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
        }).then(()=>{
            resolve()
        })
    })
}
export const MapUtil = {
    Geolocation(){
        return new Promise(resolve => {
            load().then(()=>{
                AMap.plugin('AMap.Geolocation', function() {
                    let geolocation = new AMap.Geolocation({
                        enableHighAccuracy: true, // 是否使用高精度定位，默认：true
                        timeout: 10000, // 设置定位超时时间，默认：无穷大
                        position: 'RB', //  定位按钮的排放位置,  RB表示右下
                        zoomToAccuracy: true,
                    })
                    geolocation.getCurrentPosition(function(status, data) {
                        const {position} = data
                        AMap.plugin('AMap.Geocoder', function() {
                            let geocoder = new AMap.Geocoder()
                            geocoder.getAddress(position, function (status, result) {
                                if (status === 'complete' && result.regeocode) {
                                    resolve({
                                        ...position,
                                        address: result.regeocode.formattedAddress,
                                    } || {})
                                }else{
                                    resolve(position || {})
                                }
                            });
                        })
                    })
                })
            })
        })
    },
}
